<?php

namespace Boosen\Admin\Menus\Controllers;

use Boosen\Admin\Menus\Models\Menu;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class MenuController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Menu::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->column('name')->display(function($name) {
                return '<a href="' . route(config('menus.routes.menu_items', 'menu_items') . ".index", ['mid' => $this->id]) . '">' . $name . '</a>';
            });
            $grid->slug('Slug');
            // $grid->column('name')->dispaly(function($name) {
            //     return "<a href='/admin/users/pages/{$this->id}/edit'>$title</a>";
            // });
            $grid->created_at();
            $grid->updated_at();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
            // $grid->actions(function ($actions) {

            //     // append一个操作
            //     $actions->append('<a href=""><i class="fa fa-eye"></i></a>');

            //     // prepend一个操作
            //     $actions->prepend('<a href=""><i class="fa fa-paper-plane"></i></a>');
            // });
            // echo route('menu_items.index');exit;
            // $grid->column('float_bar')->floatBar();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Menu::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', 'Name')->rules('required');
            $form->text('slug', 'Slug')->rules('required')->help($this->iconHelp());
            $states = [
                'on'  => ['value' => 1, 'text' => '关闭', 'color' => 'danger'],
                'off' => ['value' => 0, 'text' => '显示', 'color' => 'success'],
            ];
            $form->switch('disabled', 'Disabled')->states($states);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->disableViewCheck();
            $form->disableEditingCheck();
        });
    }


    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'You can use {{ menus("slug_namg") }} to get all Menus';
    }
}
